<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:ccts="urn:un:unece:uncefact:documentation:standard:CoreComponentsTechnicalSpecification:2"
	xmlns:ebforms="http://code.google.com/p/eb-forms"
	xmlns:ebfuncs="http://code.google.com/p/eb-forms/functions" version="2.0">
	
	<xsl:include href="GlobalVars.xsl" />
	
	<!-- 
		Guidelines for functions
		
		- Use funtions when you want to select existing nodes (when creating new nodes use
		   named templates for that).
		- Make sure that: "eb-funcs:f($x) is eb-funcs:f($x)" is always true when the functions 
		   returntype is node(). It's false when the function creates new nodes. This side effect can
		   cause suprises on the xpath level.
		 
		credits: Micheal Kay -> xslt 2.0 programmers reference.
	-->
	
	<xsl:function name="ebfuncs:transformMode" as="xsd:string">		
		<xsl:sequence 
			select="$global.doc.transformConfig/ebforms:transform-config/ebforms:mode/@value" />
	</xsl:function>
	
	<xsl:function name="ebfuncs:browser" as="xsd:string">		
		<xsl:sequence 
			select="$global.doc.transformConfig/ebforms:transform-config/ebforms:browser" />
	</xsl:function>
	
	<xsl:function name="ebfuncs:displayLable" as="xsd:boolean">
		<xsl:param name="den" as="xsd:string" />
		
		<xsl:variable name="den.lable.disabled" 
			select="$global.doc.annotations/annotations
			/core-component[@den = $den]/disable-lable/@value" />
		
		<xsl:value-of select="if ( $den.lable.disabled = 'true' ) then false() else true()" />
	</xsl:function>
	
	<xsl:function name="ebfuncs:instanceFile" as="xsd:anyURI">
		<xsl:sequence select="$global.doc.transformConfig/ebforms:transform-config
			/ebforms:mode/ebforms:file/@location" />
	</xsl:function>
	
	<xsl:function name="ebfuncs:getDen" as="xsd:string">
		<xsl:param name="context" />
		
		<xsl:sequence select="if( not( $context/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName ) ) then
			error( QName( 'http://code.google.com/p/eb-forms/functions', 'getDen' )
				, concat( 'Context does not have a DictionaryEntryName: ', local-name( $context ) ) )
			else $context/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName" />
	</xsl:function>
	
	<xsl:function name="ebfuncs:getStrippedDen" as="xsd:string">
		<xsl:param name="context" />
		
		<xsl:variable name="den" select="if( not( $context/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName ) ) then
			error( QName( 'http://code.google.com/p/eb-forms/functions', 'getDen' )
			, concat( 'Context does not have a DictionaryEntryName: ', local-name( $context ) ) )
			else $context/xsd:annotation/xsd:documentation/ccts:DictionaryEntryName" />
		
		<xsl:sequence select="replace( replace( $den, ' ', '' ), '\.', '__' )" />
	</xsl:function>
	
	<xsl:function name="ebfuncs:getAcronym" as="xsd:string">
		<xsl:param name="context" />
		
		<xsl:sequence select="if( not( $context/xsd:annotation/xsd:documentation/ccts:Acronym ) ) then
			error( QName( 'http://code.google.com/p/eb-forms/functions', 'getAcronym' )
				, concat( 'Context does not have a Acronym: ', local-name( $context ) ) )
			else $context/xsd:annotation/xsd:documentation/ccts:Acronym" />
	</xsl:function>
	
	<xsl:function name="ebfuncs:getBusinessTerm" as="xsd:string">
		<xsl:param name="context" />
		
		<xsl:sequence select="if( not( $context/xsd:annotation/xsd:documentation/ccts:BusinessTerm ) ) then
			error( QName( 'http://code.google.com/p/eb-forms/functions', 'getBusinessTerm' )
				, concat( 'Context does not have a BusinessTerm: ', local-name( $context ) ) )
			else $context/xsd:annotation/xsd:documentation/ccts:BusinessTerm" />
	</xsl:function>
</xsl:stylesheet>
